/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.NewCustomers;
import com.jeeplus.modules.infor.mapper.NewCustomersMapper;
import com.jeeplus.modules.infor.entity.Achievements;
import com.jeeplus.modules.infor.mapper.AchievementsMapper;
import com.jeeplus.modules.infor.entity.GoodsCustomer;
import com.jeeplus.modules.infor.mapper.GoodsCustomerMapper;
import com.jeeplus.modules.infor.entity.PriceCustomers;
import com.jeeplus.modules.infor.mapper.PriceCustomersMapper;

/**
 * 下游客户Service
 * @author 李超辉
 * @version 2019-04-28
 */
@Service
@Transactional(readOnly = true)
public class NewCustomersService extends CrudService<NewCustomersMapper, NewCustomers> {

	@Autowired
	private AchievementsMapper achievementsMapper;
	@Autowired
	private GoodsCustomerMapper goodsCustomerMapper;
	@Autowired
	private PriceCustomersMapper priceCustomersMapper;
	
	public NewCustomers get(String id) {
		NewCustomers newCustomers = super.get(id);
		newCustomers.setAchievementsList(achievementsMapper.findList(new Achievements(newCustomers)));
		newCustomers.setGoodsCustomerList(goodsCustomerMapper.findList(new GoodsCustomer(newCustomers)));
		newCustomers.setPriceCustomersList(priceCustomersMapper.findList(new PriceCustomers(newCustomers)));
		return newCustomers;
	}
	
	public List<NewCustomers> findList(NewCustomers newCustomers) {
		return super.findList(newCustomers);
	}
	
	public Page<NewCustomers> findPage(Page<NewCustomers> page, NewCustomers newCustomers) {
		return super.findPage(page, newCustomers);
	}
	
	@Transactional(readOnly = false)
	public void save(NewCustomers newCustomers) {
		super.save(newCustomers);
		for (Achievements achievements : newCustomers.getAchievementsList()){
			if (achievements.getId() == null){
				continue;
			}
			if (Achievements.DEL_FLAG_NORMAL.equals(achievements.getDelFlag())){
				if (StringUtils.isBlank(achievements.getId())){
					achievements.setCustomer(newCustomers);
					achievements.setPro(newCustomers.getPro());
					achievements.preInsert();
					achievementsMapper.insert(achievements);
				}else{
					achievements.preUpdate();
					achievementsMapper.update(achievements);
				}
			}else{
				achievementsMapper.delete(achievements);
			}
		}
		for (GoodsCustomer goodsCustomer : newCustomers.getGoodsCustomerList()){
			if (goodsCustomer.getId() == null){
				continue;
			}
			if (GoodsCustomer.DEL_FLAG_NORMAL.equals(goodsCustomer.getDelFlag())){
				if (StringUtils.isBlank(goodsCustomer.getId())){
					goodsCustomer.setCustomer(newCustomers);
					goodsCustomer.setPro(newCustomers.getPro());
					goodsCustomer.preInsert();
					goodsCustomerMapper.insert(goodsCustomer);
				}else{
					goodsCustomer.preUpdate();
					goodsCustomerMapper.update(goodsCustomer);
				}
			}else{
				goodsCustomerMapper.delete(goodsCustomer);
			}
		}
		for (PriceCustomers priceCustomers : newCustomers.getPriceCustomersList()){
			if (priceCustomers.getId() == null){
				continue;
			}
			if (PriceCustomers.DEL_FLAG_NORMAL.equals(priceCustomers.getDelFlag())){
				if (StringUtils.isBlank(priceCustomers.getId())){
					priceCustomers.setCustomer(newCustomers);
					priceCustomers.preInsert();
					priceCustomers.setPro(newCustomers.getPro());
					priceCustomersMapper.insert(priceCustomers);
				}else{
					priceCustomers.preUpdate();
					priceCustomersMapper.update(priceCustomers);
				}
			}else{
				priceCustomersMapper.delete(priceCustomers);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(NewCustomers newCustomers) {
		super.delete(newCustomers);
		achievementsMapper.delete(new Achievements(newCustomers));
		goodsCustomerMapper.delete(new GoodsCustomer(newCustomers));
		priceCustomersMapper.delete(new PriceCustomers(newCustomers));
	}
	
}